package com.njtech.pet.dao.example;

import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.ItemDao;
import com.njtech.pet.entity.Item;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author lxj
 * Date 2020-03-17 18:55
 * Description ItemDao测试类
 */
public class ItemDaoExample {
    static ItemDao itemDao;
    static Scanner scanner;
    public static void main(String[] args) {
        log();
    }

    private static void log() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        itemDao = (ItemDao) context.getBean("itemDao");

        System.out.println("准备开始测试" + itemDao.toString());

        scanner = new Scanner(System.in);

        //start flag -1 end flag 0
        int num = -1;
        System.out.println("please enter test index:");
        System.out.println("0: exit,");
        System.out.println("1: test insert,");
        System.out.println("2: test update,");
        System.out.println("3: test select.");
        while (num != 0) {
            num = scanner.nextInt();
            switch (num) {
                case 0:
                    System.out.println("bye.");break;
                case 1:
                    insert();
                    break;
                case 2:
                    update();
                    break;
                case 3:
                    select();
                    break;
                default:
                    System.err.println("undefined index, please enter again.");
            }
            System.out.println("waiting for your command: ");
        }
    }

    private static void insert() {
        Item item = new Item(null, null, "test insert", Constant.Item.TYPE_COMMODITY.getValue(), "test_subject", "test description", "tommorown", 30, 66.0, 800, "use intro", Constant.Item.STATE_VALID.getValue(), null, "test remark", "/");
        System.out.println("test inserting...");
        itemDao.insert(item);
    }

    private static void update() {
        System.out.println("item id:");
        int id = scanner.nextInt();
        Item item = new Item(id, null, "test update", null, "test_subject", "test updated description", null, 30, 86.0, 800, "use intro", Constant.Item.STATE_EXPIRED.getValue(), null, "test remark", "/");
        itemDao.updateByItemId(item);
        System.out.println("updated");
    }

    private static void select() {
        System.out.println("item id:");
        Integer id = scanner.nextInt();
        Item item = new Item();
        if (id == 0) {
            id = null;
        }
        item.setId(id);
        item.setItemName("test");
        item.setSubject("狗");
        List<String> states = new ArrayList<>();
        states.add(Constant.Item.STATE_VALID.getValue());
        states.add(Constant.Item.STATE_EXPIRED.getValue());
        List<Item> items;

        List<String> s = Arrays.asList("已下架");//"有效",
        items = itemDao.selectElastically(item,60,70,s,null,"2022-03-19","ID ASC");
        for (Item temp : items) {
            System.out.println(temp);
        }
        System.out.print("items.size  = " + items.size() + "\n");
    }
}
